# -*- coding: utf-8 -*-
#--------------------------------------------------------------------------
# Copyright (c) 2009 Barry Schwartz
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
# 
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.

#--------------------------------------------------------------------------
#
# Cython interface to libgcrypt
#

cdef extern from *:
    ctypedef unsigned int gpg_error_t

cdef extern from "gcrypt.h":

    ctypedef gpg_error_t gcry_error_t

    cdef enum gcry_md_algos:
        GCRY_MD_NONE   = 0
        GCRY_MD_MD5    = 1
        GCRY_MD_SHA1   = 2
        GCRY_MD_RMD160 = 3
        GCRY_MD_MD2    = 5
        GCRY_MD_TIGER  = 6
        GCRY_MD_HAVAL  = 7
        GCRY_MD_SHA256 = 8
        GCRY_MD_SHA384 = 9
        GCRY_MD_SHA512 = 10
        GCRY_MD_SHA224 = 11
        GCRY_MD_MD4    = 301
        GCRY_MD_CRC32  = 302
        GCRY_MD_CRC32_RFC1510 = 303
        GCRY_MD_CRC24_RFC2440 = 304
        GCRY_MD_WHIRLPOOL = 305

    cdef enum gcry_cipher_algos:
        GCRY_CIPHER_NONE        = 0
        GCRY_CIPHER_IDEA        = 1
        GCRY_CIPHER_3DES        = 2
        GCRY_CIPHER_CAST5       = 3
        GCRY_CIPHER_BLOWFISH    = 4
        GCRY_CIPHER_SAFER_SK128 = 5
        GCRY_CIPHER_DES_SK      = 6
        GCRY_CIPHER_AES         = 7
        GCRY_CIPHER_AES192      = 8
        GCRY_CIPHER_AES256      = 9
        GCRY_CIPHER_TWOFISH     = 10
        GCRY_CIPHER_ARCFOUR     = 301
        GCRY_CIPHER_DES         = 302
        GCRY_CIPHER_TWOFISH128  = 303
        GCRY_CIPHER_SERPENT128  = 304
        GCRY_CIPHER_SERPENT192  = 305
        GCRY_CIPHER_SERPENT256  = 306
        GCRY_CIPHER_RFC2268_40  = 307
        GCRY_CIPHER_RFC2268_128 = 308
        GCRY_CIPHER_SEED        = 309
        GCRY_CIPHER_CAMELLIA128 = 310
        GCRY_CIPHER_CAMELLIA192 = 311
        GCRY_CIPHER_CAMELLIA256 = 312

    cdef enum gcry_cipher_modes:
        GCRY_CIPHER_MODE_NONE   = 0 # Not yet specified.
        GCRY_CIPHER_MODE_ECB    = 1 # Electronic codebook.
        GCRY_CIPHER_MODE_CFB    = 2 # Cipher feedback.
        GCRY_CIPHER_MODE_CBC    = 3 # Cipher block chaining.
        GCRY_CIPHER_MODE_STREAM = 4 # Used with stream ciphers.
        GCRY_CIPHER_MODE_OFB    = 5 # Outer feedback.
        GCRY_CIPHER_MODE_CTR    = 6 # Counter.

    cdef enum gcry_cipher_flags:
        GCRY_CIPHER_SECURE      = 1 # Allocate in secure memory.
        GCRY_CIPHER_ENABLE_SYNC = 2 # Enable CFB sync mode.
        GCRY_CIPHER_CBC_CTS     = 4 # Enable CBC cipher text stealing (CTS).
        GCRY_CIPHER_CBC_MAC     = 8 # Enable CBC message auth. code (MAC).

    cdef enum gcry_ctl_cmds:
        GCRYCTL_SET_KEY  = 1
        GCRYCTL_SET_IV   = 2
        GCRYCTL_CFB_SYNC = 3
        GCRYCTL_RESET    = 4
        GCRYCTL_FINALIZE = 5
        GCRYCTL_GET_KEYLEN = 6
        GCRYCTL_GET_BLKLEN = 7
        GCRYCTL_TEST_ALGO = 8
        GCRYCTL_IS_SECURE = 9
        GCRYCTL_GET_ASNOID = 10
        GCRYCTL_ENABLE_ALGO = 11
        GCRYCTL_DISABLE_ALGO = 12
        GCRYCTL_DUMP_RANDOM_STATS = 13
        GCRYCTL_DUMP_SECMEM_STATS = 14
        GCRYCTL_GET_ALGO_NPKEY    = 15
        GCRYCTL_GET_ALGO_NSKEY    = 16
        GCRYCTL_GET_ALGO_NSIGN    = 17
        GCRYCTL_GET_ALGO_NENCR    = 18
        GCRYCTL_SET_VERBOSITY     = 19
        GCRYCTL_SET_DEBUG_FLAGS   = 20
        GCRYCTL_CLEAR_DEBUG_FLAGS = 21
        GCRYCTL_USE_SECURE_RNDPOOL= 22
        GCRYCTL_DUMP_MEMORY_STATS = 23
        GCRYCTL_INIT_SECMEM       = 24
        GCRYCTL_TERM_SECMEM       = 25
        GCRYCTL_DISABLE_SECMEM_WARN = 27
        GCRYCTL_SUSPEND_SECMEM_WARN = 28
        GCRYCTL_RESUME_SECMEM_WARN  = 29
        GCRYCTL_DROP_PRIVS          = 30
        GCRYCTL_ENABLE_M_GUARD      = 31
        GCRYCTL_START_DUMP          = 32
        GCRYCTL_STOP_DUMP           = 33
        GCRYCTL_GET_ALGO_USAGE      = 34
        GCRYCTL_IS_ALGO_ENABLED     = 35
        GCRYCTL_DISABLE_INTERNAL_LOCKING = 36
        GCRYCTL_DISABLE_SECMEM      = 37
        GCRYCTL_INITIALIZATION_FINISHED = 38
        GCRYCTL_INITIALIZATION_FINISHED_P = 39
        GCRYCTL_ANY_INITIALIZATION_P = 40
        GCRYCTL_SET_CBC_CTS = 41
        GCRYCTL_SET_CBC_MAC = 42
        GCRYCTL_SET_CTR = 43
        GCRYCTL_ENABLE_QUICK_RANDOM = 44
        GCRYCTL_SET_RANDOM_SEED_FILE = 45
        GCRYCTL_UPDATE_RANDOM_SEED_FILE = 46
        GCRYCTL_SET_THREAD_CBS = 47
        GCRYCTL_FAST_POLL = 48
        GCRYCTL_SET_RANDOM_DAEMON_SOCKET = 49
        GCRYCTL_USE_RANDOM_DAEMON = 50
        GCRYCTL_FAKED_RANDOM_P = 51
        GCRYCTL_SET_RNDEGD_SOCKET = 52
        GCRYCTL_PRINT_CONFIG = 53
        GCRYCTL_OPERATIONAL_P = 54
        GCRYCTL_FIPS_MODE_P = 55
        GCRYCTL_FORCE_FIPS_MODE = 56
        GCRYCTL_SELFTEST = 57

    cdef struct gcry_cipher_handle:
        pass
    ctypedef gcry_cipher_handle *gcry_cipher_hd_t

    void gcry_md_hash_buffer(int algo, void *digest, void *buffer, size_t length)

    char *gcry_strerror(gcry_error_t err)
    gcry_error_t gcry_cipher_open(gcry_cipher_hd_t *hd, int algo, int mode, unsigned int flags)
    void gcry_cipher_close(gcry_cipher_hd_t hd)
    gcry_error_t gcry_cipher_setkey(gcry_cipher_hd_t h, void *k, size_t l)
    gcry_error_t gcry_cipher_setiv (gcry_cipher_hd_t h, void *k, size_t l)
    gcry_error_t gcry_cipher_encrypt (gcry_cipher_hd_t h,
                                      unsigned char *out, size_t outsize,
                                      unsigned char *inp, size_t inlen)
    char *gcry_check_version(char *req_version)
    gcry_error_t gcry_control(gcry_ctl_cmds cmd, ...)

#--------------------------------------------------------------------------
